package com.shoppingmall.controller;

import com.shoppingmall.commons.Result;
import com.shoppingmall.execption.PasswordException;
import com.shoppingmall.model.Admin;
import com.shoppingmall.model.LoginParam;
import com.shoppingmall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/manger")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/mlogin")
    public Result login(LoginParam loginParam, HttpSession session){
        System.out.println(session);
        System.out.println("--------------------------------------");
        System.out.println(loginParam);
        try {
            try {
                //休眠800毫秒
                TimeUnit.MILLISECONDS.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Admin admin = adminService.login(loginParam);
            if(admin!=null){
                //将当前账户对象存入到session中
                session.setAttribute("admin",admin);
            }
            //封装为一个Result对象并返回
            return Result.success("登录成功",admin);
        } catch (UsernameNotFoundException e) {
            e.printStackTrace();
            //生成一个Result对象并返回
            return Result.fail(500,e.getMessage());
        } catch (PasswordException e) {
            e.printStackTrace();
            //生成一个Result对象并返回
            return Result.fail(500,e.getMessage());
        }
    }


    /**
     * 用户退出
     * @param session
     * @return
     */


}
